import Vue from 'vue';
import Router from 'vue-router';
import routes from './routers.js';
import api from '../js/core/api';
Vue.use(Router);
let router = new Router({
    mode: 'hash',
    base: '', // 根目录路径，配合./config/index.js中的设置，目的在build后，能顺利部署到服务端，与后台文件不冲突
    routes
});

// 相关验证拦截
if (api.info.routerKs) {
    /* let code = api.getQueryStringByName('code') || (api.dev ? window.localStorage.getItem('toonCode') : false);
    if (code) {
        api.info.token = code;
        if (api.dev) {
            window.localStorage.setItem('toonCode', code);
        }
    } else {
        router.push({path: '/err'});
    } */
    // 判断各组件是否需要登录拦截等
    router.beforeEach(function (to, from, next) {
        if (to.meta && to.meta.requiresAuth) {
            next();
        } else {
            next();
        }
    });
}

// 设置标题
router.afterEach(function (to, from) {
    if (to.meta && to.meta.title) {
        document.title = to.meta.title;
    }
});
export default router;
